package com.qf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.ReserveMeet;
import com.qf.entity.Task;
import com.qf.entity.User;
import com.qf.exception.OAException;
import com.qf.mapper.MeetMapper;
import com.qf.mapper.ReserveMeetMapper;
import com.qf.mapper.TaskMapper;
import com.qf.mapper.UserMapper;
import com.qf.service.ReserveMeetService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
@Slf4j
public class ReserveMeetServiceImpl implements ReserveMeetService {

    @Resource
    private ReserveMeetMapper reserveMeetMapper;

    @Resource
    private MeetMapper meetMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private TaskMapper taskMapper;
    @Override
    public List<Integer> findLeisureId(String startTime, String endTime) {

        //查询所有会议室的id
        HashSet<Integer> allId = meetMapper.findAllId();
        //查询预订时间里所有已被预订的会议室的id
        HashSet<Integer> reserveIds = reserveMeetMapper.reserveIds(startTime,endTime);

        //去重，删除allId里与reserveIds相同的元素
        allId.removeAll(reserveIds);

        if (allId==null){
            throw new OAException(199,"此时间段没有空闲会议室");
        }

        return new ArrayList<>(allId);
    }

    @Override
    @Transactional
    public void saveRecord(ReserveMeet reserveMeet) {
        reserveMeet.setApplyTime(new Date());
        Integer count=reserveMeetMapper.saveRecord(reserveMeet);
        if (count!=1){
            throw new OAException(199,"预约失败");
        }
        Task param = new Task();
        param.setApplyName(reserveMeet.getRealName());
        param.setApplyTime(new Date());
        param.setTaskId(7);
        param.setStatus(reserveMeet.getAuditStatus());
        param.setUserId(13);
        param.setEventId(reserveMeet.getId());
        int num = taskMapper.insertSelective(param);
    }

    @Override
    public PageInfo<ReserveMeet> findAll(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        List<ReserveMeet> reserveMeets = reserveMeetMapper.selectAll();
        PageInfo<ReserveMeet> pageInfo=new PageInfo<>(reserveMeets);
        return pageInfo;
    }

    @Override
    public List<ReserveMeet> selectReserveMeet(Integer[] ids) {
        List<ReserveMeet> list=new ArrayList<>();
        for (Integer id : ids) {
            ReserveMeet reserveMeet = reserveMeetMapper.selectByPrimaryKey(id);
            if (reserveMeet==null){
                log.info("【会议信息】导出失败，ids={}", Arrays.toString(ids));
                throw new OAException(199,"导出会议信息失败");
            }
            list.add(reserveMeet);
        }
        return list;
    }

    @Override
    public PageInfo<ReserveMeet> findInfoLike(Integer page, Integer size, ReserveMeet reserveMeet) {

        PageHelper.startPage(page,size);
        List<ReserveMeet> reserveMeets = reserveMeetMapper.findInfoLike(reserveMeet);
        PageInfo<ReserveMeet> pageInfo=new PageInfo<>(reserveMeets);
        return pageInfo;


    }


}
